<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 判断是否VIP
 */
namespace app\allwin\model;
use think\Model;
use app\allwin\model\VipCard;
use app\allwin\model\AllwinUser;
use app\allwin\model\AllwinConfig;
use app\common\facade\Inform;

class Vip extends Model{
    
    protected $pk     = 'id';
    protected $table  = 'ai_allwin_vip';
    protected $autoWriteTimestamp = true;
    protected $createTime = false;

    //关联用户的创客
    public function vipcard(){
        return $this->hasOne('VipCard','id','vipcard_id');
    }

    //关联用户信息
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','user_id');
    }

    //查询用户所属店铺
    public function allwinUser(){
        return $this->hasOne('AllwinUser','uid','user_id');
    }

    //查询用户所属店铺
    public function store(){
        return $this->hasOne('AllwinStore','id','store_id');
    }

    /**
     * 微信买单即开通会员
     * @param integer $user_id   当前用户ID
     * @param integer $parent_id 上级用户ID
     * @return void
     */
    public static function regVip($order){
        if(empty(json_decode($order->coupon_ids,true))){
            return;
        }
        $setting = AllwinConfig::getConfig($order->member_miniapp_id);
        if($setting->is_dopay_on_vip == 0){
            return;
        }
        //先判断是否会员
        $info = self::where(['user_id' => $order->uid])->count();
        if($info){
            return;
        }
        //读取会员类型
        $vipcard = VipCard::where(['member_miniapp_id' => $order->member_miniapp_id])->where(['is_up' => 1])->order('sort desc')->find();
        if(empty($vipcard)){
            return;
        }
        $vipdata['order_no']          = 'PAY'.$order->order_no;
        $vipdata['state']             = 1;
        $vipdata['coupon_ids']        = json_encode([]);
        $vipdata['amount']            = 0;
        $vipdata['is_up']             = $vipcard->is_up;  
        $vipdata['vipcard_id']        = $vipcard->id;
        $vipdata['update_time']       = time();
        $vipdata['is_lock']           = 0;
        $vipdata['is_diy']            = 1;
        $vipdata['store_id']          = $order->store_id;
        $vipdata['user_id']           = $order->uid;
        $vipdata['member_miniapp_id'] = $order->member_miniapp_id;
        $rel = self::insert($vipdata);
        if($rel){
            //判断是否好店员工
            $store_user = new AllwinUser;
            $rel = $store_user->where(['uid' => $order->uid])->find();
            if (empty($rel)) {
                $data['uid']      = $order->uid;
                $data['store_id'] = $order->store_id;
                return $store_user->insert($data);
            } else {
                $rel->store_id = $order->store_id;
                $rel->save();
            }
            Inform::sms($order->uid,$order->member_miniapp_id,['title' =>'业务进展通知','state' => '成功','type' => '身份变更','content' =>'您的身份已变更为'.$vipcard['name']]);
        }
        return;
    }
}